package tasks

import helper.base.PrintHelper.StringTo.toPrintln
import helper.base.ScannerHelper
import helper.base.StringHelper.countPrefix4BAndString
import helper.base.StringHelper.removeEmptyLine

object T1StringTasks {
    object ReplaceStr {
        @JvmStatic
        fun main(args: Array<String>) {
            ScannerHelper.doMap2 {
                it.replace("¬ ", "-")
            }
        }
    }

    object SplitLine {
        @JvmStatic
        fun main(args: Array<String>) {
            ScannerHelper.doMap2 {
                it.replace("。", "。\n")
            }
        }
    }

    object AboutLine {
        object JoinLine {
            @JvmStatic
            fun main(args: Array<String>) {
                "合并行".toPrintln()
                ScannerHelper.doMap2 {
                    var res = it
                    res = res.replace("\n", "")
                    res
                }
            }
        }

        object RemoveEmptyLine {
            @JvmStatic
            fun main(args: Array<String>) {
                "去除空行".toPrintln()
                ScannerHelper.doMap2 {
                    var res = it
                    while (res.contains("\n\n")) {
                        res = res.replace("\n\n", "\n")
                    }
                    res
                }
            }
        }
    }

    object Y2023 {
        object D0523T01 {
            @JvmStatic
            fun main(args: Array<String>) {
                "去除粗体".toPrintln()
                ScannerHelper.doMap2 {
                    it.replace("**", "")
                }
            }
        }

        object D0702T01 {
            @JvmStatic
            fun main(args: Array<String>) {
                "改@为_".toPrintln()
                ScannerHelper.doMap2 {
                    var res = it.replaceFirst("@", "t_")
                    res = res.replace("@", "_")
                    res = res.replace("-", "_")
                    res = res.replace(" ", "")
                    res
                }
            }
        }

        object D0906T01 {
            @JvmStatic
            fun main(args: Array<String>) {
                "将小写变为大写".toPrintln()
                ScannerHelper.doMap2 {
                    val res = it.uppercase()
                    res
                }
            }
        }

        object D123101 {
            @JvmStatic
            fun main(args: Array<String>) {
                (0..25).forEach {
                    println('a' + it)
                    println()
                }
            }
        }
    }

    object Y2024 {
        object D0310T01 {
            @JvmStatic
            fun main(args: Array<String>) {
                ScannerHelper.doMap3 {
                    it.replace(
                        "subsection",
                        "subsubsection",
                    ).replace(
                        "section",
                        "subsection",
                    ).replace(
                        "chapter",
                        "section",
                    ).replace(
                        "title",
                        "section",
                    )
                }
            }
        }

        object D0314T01 {
            @JvmStatic
            fun main(args: Array<String>) {
                ScannerHelper.doMap3 {
                    it.replace(
                        "\\( ",
                        "\$",
                    ).replace(
                        " \\)",
                        "\$",
                    )
                }
            }
        }

        object D0502T01 {
            @JvmStatic
            fun main(args: Array<String>) {
                ScannerHelper.doMap3 {
                    var res = it
                    res = res.replace("@", "t_")
                    res = res.replace("-", "_")
                    res
                }
            }
        }

        object D0502T02 {
            @JvmStatic
            fun main(args: Array<String>) {
                ScannerHelper.doMap3 {
                    var res = it
                    res = res.replace(" @", "_")
                    res = res.replace("-", "_")
                    res = res.replace("@", "t_")
                    res = res.trim()
                    res
                }
            }
        }

        object D0610T01 {
            @JvmStatic
            fun main(args: Array<String>) {
                ScannerHelper.doMap3 {
                    var res = it
                    res = res.replace("\n\n", "\n")

                    res = res.replace(" ", " ")
                    res = res.replace("\n，你的答案是：", "，你的答案是：")
                    res = res.replace("\n    ", "\n")

                    arrayOf("A", "B", "C", "D").map {
                        res = res.replace("${it}\n", "${it}.")
                    }

                    res = res.replace("收起\n", "\n")

                    res = res.replace("解析\n", "\n解析\n")

                    res = res.replace("视频\n会员专享会员专享，观看解析视频请开通会员立即开通", "")
                    res = res.replace("考点\n", "考点：")
                    res = res.replace("来源\n", "来源：")
                    res = res.replace("正确率\n", "正确率：")
                    res = res.replace("易错项\n", "易错项：")

                    res = res.removeEmptyLine()
                    res
                }
            }
        }

        object D0610T02 {
            @JvmStatic
            fun main(args: Array<String>) {
                ScannerHelper.doMap3 {
                    var res = it
                    var results = StringBuilder()

                    val strings = res.split("\n")

                    val prefixs = Array(10) { "" }

                    strings.map {
                        val (num, str) = it.countPrefix4BAndString()

                        prefixs[num] = str

                        ((num + 1) until 10).map {
                            prefixs[it] = ""
                        }


                        val result = StringBuilder()
                        result.append("@")
                        prefixs.map {
                            if (it != "") {
                                result.append("${it}_")
                            }
                        }
                        result.removeSuffix("_")
                        results.append("${result}\n")
                    }

                    var resultsStr = results.toString()

                    resultsStr = resultsStr.replace("_\n", "\n")

                    resultsStr
                }
            }
        }
    }
}